home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / allocaap.zip / ALLOCA.8 next >
Text File  |  1990-08-29  |  3KB  |  84 lines

  1. ;
  2. ; alloca.8 begins
  3. ; source for a86 assembler
  4. ; released into the public domain with the restriction that the
  5. ; acknowledgement below be preserved:
  6. ;   ...  This alloca() for Turbo C is by Alexander Pruss  ...
  7. ;
  8. ; Assemble via:
  9. ;  A86 +oc =__<MODEL-IDENTIFIER>__ alloca.8
  10. ; Note that the HUGE alloca = LARGE alloca and that TINY alloca = SMALL alloca
  11. ;
  12. #if __MEDIUM__
  13. L_CODE = 1
  14. #elseif __COMPACT__
  15. L_DATA = 1
  16. #elseif __LARGE__
  17. L_DATA = 1
  18. L_CODE = 1
  19. #elseif __HUGE__
  20. L_DATA = 1
  21. L_CODE = 1
  22. #endif
  23.  
  24. #if L_CODE
  25. ALLOCA_TEXT     segment byte public 'CODE'
  26. #else
  27. TEXT            segment byte public 'CODE'
  28. #endif
  29.  
  30. @alloca_buffer dw ?
  31.  
  32. public __alloca
  33. ;; void *_alloca(unsigned size) ;;
  34. __alloca:
  35.         pop      bx                     ; Return address
  36. #if L_CODE
  37.         pop      es                     ; into [ES:]BX
  38. #endif
  39.         pop      cx                     ; Data size.  (Argument to alloca)
  40.  
  41.         pop      dx                      ; Store some
  42.         pop      ax
  43. #if L_DATA
  44.         pop      cs:@alloca_buffer       ; data from caller's stack
  45.         push     cs:@alloca_buffer       ; (DS,SI,DI--some may not be necessary)
  46. #endif                                   ; DS unnecessary for small data models
  47.         push     ax                      ; we put it back after reading
  48.         push     dx
  49.         inc      cx                     ; Round CX to even.  (SP must be even,
  50.                                         ; else performance degrades)
  51.         shr      cx,1
  52.      shl     cx,1     
  53.         sub      sp,cx                  ; Allocate the space
  54.  
  55. #if L_DATA
  56.         push     cs:@alloca_buffer      ; make a copy of the possible data
  57. #endif
  58.         push     ax                     ; (DS,SI,DI) on the caller's stack
  59.         push     dx
  60.  
  61.         mov      ax,sp                  ; Return value.
  62. #if L_DATA
  63.         add      ax,8                   ; remember we put 3 words on the stack
  64.                                         ; and the stack points to a free word
  65. #else
  66.         add      ax,6                   ; 2 words for small data models only
  67. #endif
  68.         push     cx                     ; put the size back on the stack
  69. #if L_CODE
  70.         push     es                     ; high order return address
  71. #endif
  72.         push     bx                     ; low order return address
  73. #if L_DATA
  74.         mov      dx,ss                  ; high order return value
  75. #endif
  76. #if L_CODE
  77.         retf                            ; out we go!
  78. #else
  79.         ret
  80. #endif
  81.  
  82. ALLOCA_TEXT     ends
  83.     end
  84.